//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    name: '',
    password: '',
    scode: '',
    msg: '请绑定账号',
    userInfo: {},
    disable: true,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  nameInput: function (e) {
    this.setData({ name: e.detail.value })
    if (this.data.name != '' && this.data.password != '') {
      this.setData({ disable: false })
    } else {
      this.setData({ disable: true })
    }

  },
  pwdInput: function (e) {
    this.setData({ password: e.detail.value })
    if (this.data.name != '' && this.data.password != '') {
      this.setData({ disable: false })
    } else {
      this.setData({ disable: true })
    }
  },
  scodeInput: function (e) {
    this.setData({ scode: e.detail.value })
  },
  openTest: function () {
    wx.navigateTo({
      url: '../group/group'
    })
  },
  openLoading: function (e) {
    var that = this;
    console.log(this.data.name);
    var name = this.data.name;
    var password = this.data.password;
    var app = getApp()
    wx.showLoading({
      title: '数据加载中'
    });
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo
        console.log("用户个人信息为\n"+userInfo);
      }
    }),
      wx.login({
        success: function (res) {
          console.log(res.errMsg + "," + res.code)
          if (res.code) {
            //发起网络请求
            that.requestData(name, password, res.code, that.data.userInfo);
          } else {
            wx.showModal({
              title: '提示:' + res.errMsg,
              content: "获取用户登录态失败！",
              showCancel: false
            })
          }
        }
      })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShareAppMessage: function () {
    return {
      title: '手机群录',
      path: '/page/sign?id=123'
    }
  },
  requestData: function (name, password, code, userInfo) {
    console.log(userInfo);
    wx.request({
      url: app.globalData.mpurl + 'user/login',
      method: "POST",
      dataType: 'json',
      data: {
        username: name,
        password: password,
        wxCode: code,
        // userInfo: userInfo,
        nickName:userInfo.nickName,
        avatarUrl: userInfo.avatarUrl
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        if (res.code == '00') {
          wx.setStorageSync('refush', true),
          wx.setStorageSync('openId', res.data.openId);
          wx.setStorageSync('sessionKey', res.data.sessionKey);
          wx.switchTab({
            url: '../group/group',
            success: function (res) {
              console.log("跳转成功");
            },
            fail: function (res) {
              console.log("失败");
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '账号或密码有误',
            showCancel: false,
            success: function (res) {
            }
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: "系统异常，请稍候再试",
          showCancel: false
        })
      }
    });
  }
})
